package com.vm.db;

import java.util.ArrayList;
import java.util.List;

import com.vm.model.DownInfo;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DownInfoDao {

	private static DBOpenHelper helper = null;
	static String TAG = "InfoDao";
//	SQLiteDatabase db = null;

	public DownInfoDao(Context context) {
		if (DBOpenHelper.db == null) {
			helper = new DBOpenHelper(context);
//			db = DBOpenHelper.db;
		}
	}

	public synchronized void insert(DownInfo info) {
		Log.d(TAG, "insert");

		try {
			DBOpenHelper.db.execSQL(
					"INSERT INTO downInfo (path, softId,name,imageUrl,isPause, done,apkLength,completed) VALUES(?, ?,?,?, ?,?,?,?)",
					new Object[] { info.getPath(), info.getSoftId(),
							info.getName(), info.getImageUrl(),
							info.getIsPause(), info.getDone(),
							info.getApkLength(), 0 });
		} catch (SQLException e) {
			Log.d(TAG, "insert---" + e.getMessage());
		}
	}

	/**
	 * 更新下载状态(完成/为完成),下载结束后done重置为0
	 * 
	 * @param softId
	 */
	public void complete(String softId) {
		Log.d(TAG, "complete");
		try {
			DBOpenHelper.db.execSQL(
					"update downInfo set completed=1,done=0 WHERE softId=? ",
					new Object[] { softId });
		} catch (SQLException e) {
			Log.d(TAG, "complete---" + e.getMessage());
		}
	}

	public synchronized void update(DownInfo info) {
		Log.d(TAG, "update");
		try {
			DBOpenHelper.db.execSQL(
					"UPDATE downInfo SET done=?,isPause=?,completed=? WHERE path=?  AND softId=?",
					new Object[] { info.getDone(), info.getIsPause(), 0,
							info.getPath(), info.getSoftId() });
		} catch (SQLException e) {
			Log.d(TAG, "update---" + e.getMessage());
		}
	}

	/**
	 * 暂停所有下载任务
	 */
	public synchronized void stopAll() {
		Log.d(TAG, "stopAll");
		try {
			DBOpenHelper.db.execSQL("UPDATE downInfo SET isPause=1 WHERE completed=0");
		} catch (SQLException e) {
			Log.d(TAG, "stopAll---" + e.getMessage());
		}
	}

	/**
	 * 暂停所有下载任务
	 */
	public synchronized void start(String softId) {
		Log.d(TAG, "start");
		try {
			DBOpenHelper.db.execSQL("UPDATE downInfo SET isPause=0 WHERE softId=?",
					new Object[] { softId });
		} catch (SQLException e) {
			Log.d(TAG, "start---" + e.getMessage());
		}
	}

	public synchronized List<DownInfo> getDownloaded() {
		List<DownInfo> list = new ArrayList<DownInfo>();
		Log.d(TAG, "getDownloaded");
		Cursor c = null;
		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT path, softId,name,imageUrl,isPause, done,apkLength,completed FROM downInfo where completed=1",
					null);
			while (c.moveToNext()) {
				DownInfo info = new DownInfo();
				info.setPath(c.getString(0));
				info.setSoftId(c.getString(1));
				info.setName(c.getString(2));
				info.setImageUrl(c.getString(3));
				info.setIsPause(c.getInt(4));
				info.setDone(c.getInt(5));
				info.setApkLength(c.getInt(6));
				info.setCompleted(c.getInt(7));
				list.add(info);
			}

		} catch (Exception e) {
			Log.d(TAG, "getDownloaded---" + e.getMessage());
		} finally {
			if(c!=null){
				c.close();
			}
		}
		return list;
	}

	public synchronized List<DownInfo> getDownloading() {
		List<DownInfo> list = new ArrayList<DownInfo>();
		Log.d(TAG, "query");
		Cursor c = null;
		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT path, softId,name,imageUrl,isPause, done,apkLength,completed FROM downInfo where completed=0",
					null);
			while (c.moveToNext()) {
				DownInfo info = new DownInfo();
				info.setPath(c.getString(0));
				info.setSoftId(c.getString(1));
				info.setName(c.getString(2));
				info.setImageUrl(c.getString(3));
				info.setIsPause(c.getInt(4));
				info.setDone(c.getInt(5));
				info.setApkLength(c.getInt(6));
				info.setCompleted(c.getInt(7));
				list.add(info);
			}

		} catch (Exception e) {
			Log.d(TAG, "getDownloading---" + e.getMessage());
		} finally {
			if(c!=null){
				c.close();
			}
		}

		return list;
	}

	public synchronized DownInfo query(String softId) {
		Log.d(TAG, "query");
		Cursor c = null;
		DownInfo info = null;
		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT path, softId,name,imageUrl,isPause, done,apkLength,completed FROM downInfo WHERE  softId=?",
					new String[] { softId });
			info = null;
			if (c.moveToNext()) {
				info = new DownInfo();
				info.setPath(c.getString(0));
				info.setSoftId(c.getString(1));
				info.setName(c.getString(2));
				info.setImageUrl(c.getString(3));
				info.setIsPause(c.getInt(4));
				info.setDone(c.getInt(5));
				info.setApkLength(c.getInt(6));
				info.setCompleted(c.getInt(7));
			}
		} catch (Exception e) {
			Log.d(TAG, "query---" + e.getMessage());
		} finally {
			if(c!=null){
				c.close();
			}
		}
		return info;
	}

	public synchronized void delete(String path, String softId) {
		Log.d(TAG, "delete");
		try {
			DBOpenHelper.db.execSQL("DELETE FROM downInfo WHERE path=? and softId=?",
					new Object[] { path, softId });
		} catch (SQLException e) {
			Log.d(TAG, "delete---" + e.getMessage());
		}
	}

	public List<String> queryUndone() {
		Log.d(TAG, "queryUndone");
		Cursor c = null;
		List<String> pathList = new ArrayList<String>();
		try {
			c = DBOpenHelper.db.rawQuery("SELECT DISTINCT path FROM downInfo", null);
			pathList = new ArrayList<String>();
			while (c.moveToNext())
				pathList.add(c.getString(0));
		} catch (Exception e) {
			Log.d(TAG, "queryUndone---" + e.getMessage());
		} finally {
			if(c!=null){
				c.close();
			}
		}
		return pathList;
	}

}